<template>
	<div>
		<el-dialog title="修改" width="70%" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form" :model="form" :rules="rules" :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">
 				 <el-row >
					<el-col :span="24">
						<el-form-item label="登录账号" prop="login_name">
							<el-select style="width:100%" v-model="form.login_name" filterable clearable placeholder="请选择">
								<el-option v-for="(item,i) in login_names" :key="i" :label="item.key" :value="item.val.toString()"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="登录IP地址" prop="ipaddr">
							<el-input v-model="form.ipaddr" autoComplete="off" clearable  placeholder="请输入登录IP地址"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="登录地点" prop="login_location">
							<el-input v-model="form.login_location" autoComplete="off" clearable  placeholder="请输入登录地点"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="浏览器类型" prop="browser">
							<el-input v-model="form.browser" autoComplete="off" clearable  placeholder="请输入浏览器类型"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="操作系统" prop="os">
							<el-input v-model="form.os" autoComplete="off" clearable  placeholder="请输入操作系统"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="登录状态" prop="status">
							<el-radio-group v-model="form.status">
                                <el-radio :label="0">登录成功</el-radio>
                                <el-radio :label="1">登录失败</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				 </el-row>  
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="提示消息" prop="msg">
							<el-input v-model="form.msg" autoComplete="off" clearable  placeholder="请输入提示消息"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item label="访问时间" prop="login_time">
							<el-date-picker type="date" v-model="form.login_time" clearable placeholder="请输入访问时间"></el-date-picker>
						</el-form-item>
					</el-col>
				 </el-row>
				</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">确 定</span>
					<span v-else>提 交 中...</span>
				</el-button>
				<el-button :size="size" @click="closeForm">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { update, getFieldList } from '@/api/admin/logininfor'

export default {
	name:'logininforupdate',
	components: {
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
		info: {
			type: Object,
		},
	},
	data(){
		return {
			form: {
			   login_name:'',
			   ipaddr:'',
			   login_location:'',
			   browser:'',
			   os:'',
			   status:'',
			   msg:'',
			   login_time:'',
			},
			login_names:[],
			loading:false,
			rules: {
			   browser:[
			     {required: true, message: '浏览器类型不能为空', trigger: 'blur'},
			     {pattern:/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/, message: '浏览器类型格式错误'}
			   ],
			}
		}
	},
	watch:{
		show(val){
			if(val){
				getFieldList().then(res => {
					if(res.status == 200){
						this.login_names = res.data.login_names
					}
				})
			}
		}
	},
	methods: {
		open(){
			this.form = this.info
		},
		submit(){
			this.$refs['form'].validate(valid => {
				if(valid) {
					this.loading = true
					update(this.form).then(res => {
						if(res.status == 200){
							this.$message({message: '操作成功', type: 'success'})
							this.$emit('refresh_list')
							this.closeForm()
						}
					}).catch(()=>{
						this.loading = false
					})
				}
			})
		},
		closeForm(){
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form']!==undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common.scss'
</style>
